package com.cf.routing;


import com.cf.utils.ConnectionUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * work 工作模式
 *
 * @author Chengfei
 * @date 2020/8/29 15:08
 */
public class Consumer1 {


    public static void main(String[] args) throws Exception {

        // 连接对象
        Connection connection = ConnectionUtil.getConnection();
        //创建频道
        Channel channel = connection.createChannel();

        //声明交换机
        channel.exchangeDeclare(Produce.DIRECT_EXCHAGE,BuiltinExchangeType.DIRECT);

        //
        channel.queueDeclare(Produce.DIRECT_QUEUE_INSERT,true,false,false,null);

        //队列绑定交换机
        channel.queueBind(Produce.DIRECT_QUEUE_INSERT,Produce.DIRECT_EXCHAGE,"insert");


        //创建消费者,并设置消息处理
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            /**
             * consumerTag 消息者标签，在channel.basicConsume时候可以指定
             * envelope 消息包的内容，可从中获取消息id，消息routingkey，交换机，消息和重传标志(收到消息失败后是否需要重新发送)
             * properties 属性信息
             * body 消息
             */
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //路由key
                System.out.println("路由key为：" + envelope.getRoutingKey());
                //交换机
                System.out.println("交换机为：" + envelope.getExchange());
                //消息id
                System.out.println("消息id为：" + envelope.getDeliveryTag());
                //收到的消息
                System.out.println("接收到的消息为：" + new String(body, "utf-8"));

            }
        };

        //channel.basicConsume("", true, consumer);

        /**
         * 监听消息
         * 参数1：队列名称
         * 参数2：是否自动确认，设置为true为表示消息接收到自动向mq回复接收到了，mq接收到回复会删除消息，设置为false则需要手动确认
         * 参数3：消息接收到后回调
         */
        channel.basicConsume(Produce.DIRECT_QUEUE_INSERT, true, consumer);
        //6 不关闭资源
        //channel.close();
        //connection.close();

    }
}
